﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WebApiTool
{
    public class IndexWriter
    {
        StringBuilder _stringBuilder = new StringBuilder();

        public void Begin()
        {
            _stringBuilder.Append("<!DOCTYPE html>");
            _stringBuilder.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            _stringBuilder.Append("<head>");
            _stringBuilder.Append("<title></title>");
            _stringBuilder.AppendLine("<style>");
            _stringBuilder.AppendLine(".lv1 { margin: 0px; padding-left: 0px; font-size: 1.2em;  }");
            _stringBuilder.AppendLine(".lv2 { margin: 0px; padding-left: 0px; font-size: 1.0em;  }");
            _stringBuilder.AppendLine(".lv3 { margin: 0px; padding-left: 0px; font-size: 0.8em;  }");
            _stringBuilder.AppendLine(".lv3 li {  list-style: inside decimal-leading-zero; margin:0px; padding:0px }");
            _stringBuilder.Append("</style>");
            _stringBuilder.Append("</head>");

            _stringBuilder.Append("<body>");

            _stringBuilder.Append("<div style=\"position: fixed; left: 0; top: 0; bottom: 0; width: 250px; overflow-y:scroll\">");

            _stringBuilder.Append("<ul class='lv1'>");
        }

        public void End()
        {
            _stringBuilder.Append("</ul>");
            _stringBuilder.Append("</div>");
            _stringBuilder.Append("<div style=\"position: fixed; left: 250px; top: 0; bottom: 0; right: 0;\">");
            _stringBuilder.Append("<iframe name=\"contentFrame\" id=\"contentFrame\" style=\"width:100%; height:100%;\"></iframe>");
            _stringBuilder.Append("</div>");
            _stringBuilder.Append("</body>");
            _stringBuilder.Append("</html>");
        }

        public void Build(DModule module)
        {
            _stringBuilder.Append("<li>");
            _stringBuilder.Append(module.Name);
            _stringBuilder.Append("<ul class='lv2'>");

            foreach (var scenario in module.ScenarioList)
            {
                _stringBuilder.Append("<li>");
                _stringBuilder.Append(scenario.Name);
                _stringBuilder.Append("<ul class='lv3'>");
                foreach (var action in scenario.Actions)
                {
                    _stringBuilder.Append("<li>");
                    _stringBuilder.AppendFormat("<a href='./Docs/{0}_{1}_{2}.html'  target=\"contentFrame\">", module.Name, scenario.Name, action.Name);
                    _stringBuilder.Append(action.Name);
                    _stringBuilder.Append("</a>");
                    _stringBuilder.Append("</li>");
                }
                _stringBuilder.Append("</ul>");
                _stringBuilder.Append("</li>");
            }

            _stringBuilder.Append("</ul>");
            _stringBuilder.Append("</li>");
        }

        public void Save(string filePath)
        {
            File.WriteAllText(filePath, _stringBuilder.ToString());
        }
    }
}
